import wepy from 'wepy'
export default class extends wepy.mixin {
  data = {
    // 会员id
    id: '',
    pageIndex: '1',
    pageSize: '200',
    // 收藏数据列表
    getgoodsListInfo: [],
    total: 1000,
    urls: wepy.$instance.globalData.baseURl,
    zhanweit: '2020-05-07/202005071101421588820502650_d2l0h.png',
  }
  methods = {
    async deletes(goodsid) {
      console.log(goodsid)
      const res = await wepy.showModal({
        title: '取消收藏',
        content: '确定取消收藏吗？',
      })
      if (res.confirm) {
        this.removeGoods(goodsid)
      }
    },
    // 跳转到商品详情
    goGoodsDatils(id) {
      wepy.navigateTo({
        url: '../../pages/goodsDatils/gooddatils?id=' + id,
      })
    },
    guangGuang() {
      console.log('去逛逛')
      wepy.switchTab({
        url: '../../pages/tabs/home',
      })
    },
  }
  computed = {
    // 判断页面是否有商品加入
    isEmpty() {
      if (this.getgoodsListInfo.length <= 0) {
        return true
      }
      return false
    },
    isTotal() {
      if (this.getgoodsListInfo != null) {
        let numm = this.getgoodsListInfo.length
        return numm
      }
    },
  }
  onLoad() {
    console.log('收藏页面启动了')
    let that = this
    let cid = wepy.getStorageSync('userId')
    that.id = cid
  }
  onShow() {
    this.getgoodsList()
  }
  // 获取收藏数据
  async getgoodsList() {
    const res = await wepy.post('baseRest/getMemberSpuAss', {
      memberId: this.id,
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
    })
    console.log(res)
    if (res.statusCode != 200) {
      return wepy.noToast('获取 收藏列表失败')
    }
    // this.getgoodsListInfo = [...this.getgoodsListInfo, ...res.data.data]
    this.getgoodsListInfo = res.data.data
    this.$apply()
  }
  // 取消收藏
  async removeGoods(goodsid) {
    const res = await wepy.post('baseRest/saveMemberSpuAss', {
      memberId: this.id,
      spuId: goodsid,
    })
    console.log(res)
    if (res.data.state == 1) {
      this.getgoodsListInfo = []
      this.pageIndex = '1'
      this.pageSize = '100'
      wepy.baseToast('取消收藏成功')
      this.getgoodsList()
    } else {
      wepy.baseToast(res.data.message)
    }
  }
   returnFloat(value){
    var value=Math.round(parseFloat(value)*100)/100;
    var xsd=value.toString().split(".");
    if(xsd.length==1){
    value=value.toString()+".00";
    return value;
    }
    if(xsd.length>1){
    if(xsd[1].length<2){
    value=value.toString()+"0";
    }
    return value;
    }
   }
  // 触底触发的事件
  onReachBottom() {
    // console.log('已触底')
    // if (this.pageIndex * this.pageSize >= this.total) {
    // 	return
    // }
    // let num = parseInt(this.pageIndex)
    // num++
    // this.pageIndex = num.toString()
    // this.getgoodsList()
  }
  // 下拉刷新触发的事件，重置，发送请求
  // onPullDownRefresh() {
  //   console.log('下拉已刷新')
  // }
}
